package com.hulk.dryad.admin.controller;

import com.hulk.dryad.common.annotation.PrintlnLog;
import com.hulk.dryad.common.component.R;
import com.hulk.dryad.common.constant.SecurityConstants;
import com.hulk.dryad.common.util.SecurityUtils;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * <p>
 *
 * @author kim
 * @date 2021/3/23
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(SecurityConstants.LOGOUT_URL)
public class SysLogoutController {

	private final LogoutSuccessHandler dryadLogoutSuccessHandler;

	@ApiOperation(value = "logout", notes = "用户退出系统")
	@PrintlnLog
	@DeleteMapping
	public R logout(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		Authentication authentication = SecurityUtils.getAuthentication();
		dryadLogoutSuccessHandler.onLogoutSuccess(request, response, authentication);
		return R.ok();
	}

}
